package com.lihepeng.leecode.juc;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池 执行测试类
 * 默认情况下会跑出如下异常
 * AbortPolicy.rejectedExecution
 */
class work implements Runnable{

    @Override
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName()+"---->working");
            if (!"main".equals(Thread.currentThread().getName())) {
                TimeUnit.SECONDS.sleep(1);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public class ThreadPoolExecutorDemo {

    public static void main(String[] args) {
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
        ThreadPoolExecutor threadPoolExecutor =
                new ThreadPoolExecutor(3,6,100, TimeUnit.MILLISECONDS
                        ,arrayBlockingQueue,new ThreadPoolExecutor.AbortPolicy());
        for (int i=0;i<20;i++) {
            threadPoolExecutor.submit(new work());
        }
        threadPoolExecutor.shutdown();
    }
}
